#include <bits/stdc++.h>

#define rep(i, j, k) for (int i = (j); i <= (k); ++i)
#define per(i, j, k) for (int i = (j); i >= (k); --i)
#define SZ(v) int((v).size())
#define ALL(v) (v).begin(),(v).end()
#define fi first
#define se second
using ll = long long;
using pii = std::pair<int, int>;
using pll = std::pair<ll, ll>;

template<class T>inline void chkmn(T &x, T y) { if (y < x) x = y; }
template<class T>inline void chkmx(T &x, T y) { if (y > x) x = y; }

using namespace std;

template <int P>
class mod_int {
  using Z = mod_int;

private:
  static int mo(int x) { return x < 0 ? x + P : x; }

public:
  int x;
  int val() const { return x; }
  mod_int() : x(0) {}
  template <class T>
  mod_int(const T &x_) : x(x_ >= 0 && x_ < P ? static_cast<int>(x_) : mo(static_cast<int>(x_ % P))) {}
  bool operator==(const Z &rhs) const { return x == rhs.x; }
  bool operator!=(const Z &rhs) const { return x != rhs.x; }
  Z operator-() const { return Z(x ? P - x : 0); }
  Z pow(long long k) const {
    Z res = 1, t = *this;
    while (k) {
      if (k & 1) res *= t;
      if (k >>= 1) t *= t;
    }
    return res;
  }
  Z &operator++() {
    x < P - 1 ? ++x : x = 0;
    return *this;
  }
  Z &operator--() {
    x ? --x : x = P - 1;
    return *this;
  }
  Z operator++(int) {
    Z ret = x;
    x < P - 1 ? ++x : x = 0;
    return ret;
  }
  Z operator--(int) {
    Z ret = x;
    x ? --x : x = P - 1;
    return ret;
  }
  Z inv() const { return pow(P - 2); }
  Z &operator+=(const Z &rhs) {
    (x += rhs.x) >= P && (x -= P);
    return *this;
  }
  Z &operator-=(const Z &rhs) {
    (x -= rhs.x) < 0 && (x += P);
    return *this;
  }
  Z &operator*=(const Z &rhs) {
    x = 1ULL * x * rhs.x % P;
    return *this;
  }
  Z &operator/=(const Z &rhs) { return *this *= rhs.inv(); }
#define setO(T, o)                                 \
  friend T operator o(const Z &lhs, const Z &rhs) {\
    Z res = lhs;                                   \
    return res o## = rhs;                          \
  }
  setO(Z, +) setO(Z, -) setO(Z, *) setO(Z, /)
#undef setO
};
const int P = 998244353;
using Z = mod_int<P>;

#define double long double

const int maxn = 10000010;

struct num {
  Z k, b;
  num() { k = b = 0; }
  num(Z x, Z y) { k = x, b = y; }
  friend num operator+(num x, num y) {
    return num(x.k + y.k, x.b + y.b);
  }
  friend num operator-(num x, num y) {
    return num(x.k - y.k, x.b - y.b);
  }
} dq[maxn], dp[maxn];

int n, tot, a[maxn], b[maxn], cnt[maxn], A, B, C, M;
double f[maxn], g[maxn];

bool check(double mid) {
  rep (i, 1, tot) g[i] = (b[i] > 1 ? 1 - mid : 1);
  f[1] = g[1]; // f[i]: 猜 > 的概率
  rep (i, 2, tot) {
    if (2 * mid - f[i - 1] > 0) f[i] = g[i] - 2 * mid + f[i - 1];
    else f[i] = g[i];
    if (f[i] < 0) return 0;
  }
  return 1;
}

void work() {
  cin >> n >> a[0] >> A >> B >> C >> M;
  rep (i, 1, n) a[i] = (1ll * A * a[i - 1] % M * a[i - 1] % M + 1ll * B * a[i - 1] % M + C) % M + 1;
  // rep (i, 1, n) cerr << a[i] << " \n"[i == n];
  rep (i, 1, M) cnt[i] = 0;
  rep (i, 1, n) cnt[a[i]]++;
  tot = 0;
  rep (i, 1, M) if (cnt[i]) b[++tot] = cnt[i];
  // rep (i, 1, tot) cerr << b[i] << " \n"[i == tot];
  double l = 0, r = 1;
  rep (_, 1, 50) {
    double mid = (l + r) * .5;
    if (check(mid)) l = mid;
    else r = mid;
  }
  double res = r;
  rep (i, 1, tot) dq[i] = (b[i] > 1 ? num(-1, 1) : num(0, 1));
  rep (i, 1, tot) g[i] = (b[i] > 1 ? 1 - res : 1);
  dp[1] = dq[1], f[1] = g[1];
  rep (i, 2, tot) {
    if (2 * res - f[i - 1] >= 0) f[i] = g[i] - 2 * res + f[i - 1], dp[i] = dq[i] - num(2, 0) + dp[i - 1];
    else f[i] = g[i], dp[i] = dq[i];
  }
  int id = tot;
  rep (i, 1, tot - 1) if (f[i] < f[id]) id = i;
  Z ans = Z(0) - dp[id].b / dp[id].k;
  cout << res << ' ' << ans.val() << '\n';
}

int main() {
  freopen("guess.in", "r", stdin);
  freopen("guess.out", "w", stdout);
  cin.tie(nullptr) -> ios::sync_with_stdio(false);
  cout << fixed << setprecision(9);
  int t; cin >> t;
  while (t--) work();
}